package org.happycomp.radiog.encoding;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
import org.happycomp.radio.io.IOUtils;
import org.happycomp.radiog.Activator;


/**
 * Utility class for encoding ...
 * @author pavels
 */
public class EncodingUtils {

	public static final Logger LOGGER = Logger.getLogger(EncodingUtils.class.getName());
	
	public static StringTemplateGroup stGroup() {
		InputStream stream = EncodingUtils.class.getClassLoader().getResourceAsStream("org/happycomp/radiog/res/encoding.stg");
		StringTemplateGroup stg = new StringTemplateGroup(new InputStreamReader(stream), DefaultTemplateLexer.class);
		return stg;
	}

	
	public static void encodeToWav(File[] inputFiles, File exportedFile, File randomFolder, Map<String, String>commands) {
		try {
			StringTemplateGroup stGroup = stGroup();
			StringTemplate st = stGroup.getInstanceOf("toWAV");
			setTemplateAttributes(inputFiles, exportedFile, null, commands, st);
			startEncoding(randomFolder, st);
		} catch (IOException e) {
			LOGGER.log(Level.SEVERE,e.getMessage(),e);
		}
	}

	public static void encodeToWavAndThenMP3(File[] inputFiles, File wavFile, File mp3File, boolean deleteOnExit, File randomFolder, Map<String, String> commands) {
		try {
			String templateName = deleteOnExit ? "toMP3" : "toMP3_delete";
			StringTemplateGroup stGroup = stGroup();
			StringTemplate st = stGroup.getInstanceOf(templateName);
			setTemplateAttributes(inputFiles, wavFile, mp3File, commands, st);
			startEncoding(randomFolder, st);
		} catch (IOException e) {
			LOGGER.log(Level.SEVERE,e.getMessage(),e);
		}
	}


	private static void startEncoding(File randomFolder, StringTemplate st)
			throws IOException {
		File batchFile = new File(randomFolder, "process.bat");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(batchFile);
			fos.write(st.toString().getBytes(Charset.forName("windows-1250")));
		} finally {
			if (fos != null) fos.close();
		}

		String[] programsArgs = {"cmd", "/c", "rundll32","shell32.dll", "ShellExec_RunDLL", 
				batchFile.getAbsolutePath(),
		};

		System.out.println("Program and arguments :"+Arrays.asList(programsArgs));
		ProcessBuilder processBuilder = new ProcessBuilder(programsArgs);
		processBuilder.directory(new File(Activator.baseDir()));
		Process start = processBuilder.start();
		LOGGER.info("started process");
		ByteArrayOutputStream bos  =new  ByteArrayOutputStream();
	}


	private static void setTemplateAttributes(File[] inputFiles, File wavFile,
			File mp3File, Map<String, String> commands, StringTemplate st) {
		st.setAttribute("inputFiles", inputFiles);
		st.setAttribute("outputFile", wavFile);
		if (mp3File != null) st.setAttribute("mp3File", mp3File);
		st.setAttribute("commands", commands);
	}

	public static void main(String[] args) {
		File[] fls = new File[] {
			new File("CRO1"),
			new File("CRO1_1"),
			new File("CRO1_2")
		};

//		StringTemplateGroup stGroup = stGroup();
//		StringTemplate st = stGroup.getInstanceOf("toWav");
//		st.setAttribute("inputFiles", fls);
//		st.setAttribute("outputFile", new File("Test.wav"));
//		System.out.println(st.toString());
		
		StringTemplateGroup stGroup = stGroup();
		StringTemplate st = stGroup.getInstanceOf("toMP3");
		st.setAttribute("inputFiles", fls);
		st.setAttribute("outputFile", new File("Test.wav"));
		st.setAttribute("mp3File", new File("Test.mp3"));
		System.out.println(st.toString());

	}
}


